package spring6_learn.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring6_learn.JwtUtil;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SessionController {
    @RequestMapping("/login")
    public String login(HttpServletRequest request)
    {

        Map<String,Object> claims = new HashMap<>();
        claims.put("username","zhangsan");
        claims.put("password","123456");
        String token = JwtUtil.generateToken(claims, 1000 * 60 * 60 * 24 * 7);
        return token;
    }
    //设置cookie
    @RequestMapping("/setCookie")
    public void setCookie(HttpServletResponse response)
    {
        response.addCookie(new Cookie("username","zhangsan")
        );

    }
    //获取cookie
    @RequestMapping("/getCookie")
    public void getCookie(HttpServletRequest request)
    {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("username"))
            {
                System.out.println("cookie.getValue() = " + cookie.getValue());
            }
        }
    }
}
